package com.savitar.dict.service;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.savitar.common.annotation.Create;
import com.savitar.common.annotation.Update;
import com.savitar.dict.dao.DictMapper;
import com.savitar.dict.entity.Dict;
import com.savitar.dict.entity.DictType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Dict)表服务接口
 *
 * @author Savitar
 * @since 2021-05-19 21:17:48
 */
@Service
public class DictService extends ServiceImpl<DictMapper, Dict> {

    public Page<Dict> page(Integer pageNum, Integer pageSize, String typeId, String key) {
        Page<Dict> page = new Page<>(pageNum, pageSize);
        return lambdaQuery().eq(StringUtils.isNotBlank(key), Dict::getName, key)
                .eq(Dict::getDictType, typeId).orderByAsc(Dict::getOrderNum).page(page);
    }

    @Create
    public Dict create(Dict createVo) {
        createVo.setPkId(IdUtil.simpleUUID());
        createVo.insert();
        return createVo;
    }

    @Update
    public Dict update(Dict updateVo) {
        updateVo.updateById();
        return updateVo;
    }

    public Dict deleteById(String id) {
        Dict one = lambdaQuery().eq(Dict::getPkId, id).one();
        one.deleteById();
        return one;
    }

    public List<Dict> listByType(String type) {
        return lambdaQuery()
                .eq(Dict::getDictType, type)
                .eq(Dict::getIsEnabled, "01").list();
    }
}